// File (fram emulation) access with resource blocking, use as RAII

#ifndef FILESTORAGE_H
#define FILESTORAGE_H

#include <stdint.h>
#include <stdio.h>
#include "common/Mutex.h"
#include "lockablestorage.h"

class FileStorage : public LockableStorage {
public:	
	FileStorage(const char *filename, uint32_t capacity);
	virtual ~FileStorage();

	virtual bool lock();
	virtual void unlock();
	virtual	bool read(uint32_t addr, void *data, uint16_t dataSize);
	virtual bool write(uint32_t addr, const void *data, uint16_t dataSize);

private:
	FileStorage();
	bool init();
	void deinit();
	bool checkBounds(uint32_t addr, uint16_t size);
	bool openExisting();
	bool createNew();

	bool initialized_;
	char *filename_;
	const uint32_t capacity_;
	Mutex mutex_;
	FILE *file_;
};

#endif
